const scoreDom = document.createElement("div");
class Game {
    constructor() {
        this.sky = new Sky();
        this.land = new Land(-100);
        this.bird = new Bird();
        this.pipePareProducer = new PipePareProducer(-100);
        this.timer = null;
        this.tick = 16;  //移动时间间隔，毫秒
        this.num = 0;
        this.scoreKey = false;
    }

    start() {
        if (this.timer) {
            return;
        }
        this.pipePareProducer.startProduce(); //生成柱子
        this.bird.flying();
        this.isScore()
        this.timer = setInterval(() => {
            const duration = this.tick / 1000;
            this.sky.move(duration);
            this.land.move(duration);
            this.bird.move(duration);
            this.pipePareProducer.pairs.forEach(pair => {
                pair.move(duration);
            })
            if (this.isGameOver()) {
                this.stop();
                window.location.reload();
            }
            if (this.scoreKey) {
                this.num++;
                this.scoreKey = false;
                scoreDom.remove();
                this.isScore();
            }
        }, this.tick);
    }
    isScore() {
        scoreDom.className = "score";
        scoreDom.innerText = `分数：${this.num}`;
        gameDom.appendChild(scoreDom);
    }
    /**
     * 判断两个矩形是否碰撞
     * @param {*} rec1 
     * @param {*} rec2 
     */
    isHit(rec1, rec2) {
        //横向：两个矩形的中心点的距离，是否小于矩形宽度之和的一半
        //纵向：两个矩形的中心点的距离，是否小于矩形高度之和的一半
        let centerX1 = rec1.left + rec1.width / 2;
        let centerX2 = rec1.top + rec1.height / 2;
        let centerY1 = rec2.left + rec2.width / 2;
        let centerY2 = rec2.top + rec2.height / 2;

        let disX = Math.abs(centerX1 - centerY1); //中心点横向距离
        let disY = Math.abs(centerX2 - centerY2); //中心点纵向距离
        if (disX < (rec1.width + rec2.width) / 2 && disY < (rec1.height + rec2.height) / 2) {
            return true;
        }
        return false;
    }
    isGameOver() {
        if (this.bird.top === this.bird.maxY) {
            //鸟碰到了大地
            return true;
        }
        let pairsArr = this.pipePareProducer.pairs;
        for (let i = 0; i < pairsArr.length; i++) {
            const pair = pairsArr[i];
            console.log(this.bird.left)
            //鸟碰到得分线
            if (this.bird.left > pair.upPipe.left && pair.upPipe.left > this.bird.left - 2) {
                this.scoreKey = true;
            }
            //看柱子对有没有发生碰撞
            if (this.isHit(this.bird, pair.upPipe) || this.isHit(this.bird, pair.downPipe)) {
                return true;
            }

        }
        return false;
    }
    stop() {
        clearInterval(this.timer);
        this.timer = null;
        this.bird.stopFlying();
        this.pipePareProducer.stopProduce();
    }
    regEvent() {
        window.onkeydown = (e) => {
            if (e.key === "Enter") {
                if (this.timer) {
                    this.stop();
                } else {
                    this.start()
                }
            } else if (e.key === " ") {
                this.bird.jump();
            }
        }

    }
}
var g = new Game();
g.regEvent();